// pages/ordersure/ordersure.js
var app = getApp();
var c = require('../common.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPayPwdInput: false, //是否展示密码输入层
    pwdVal: '', //输入的密码
    payFocus: true, //文本框焦点
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.showInputLayer();
    var that = this;
    var goods_id = options.goods_id;
    var money = options.money;
    var vip = options.vip;
    var tuan = options.tuan;
    var yuyue = options.yuyue;
    var user_id = app.u.user_id;
    if (typeof (vip) == 'undefined') {
      vip = 0;
    }
    if (typeof (tuan) == 'undefined') {
      tuan = 0;
    }
    that.setData({ money:money,goods_id:goods_id,vip:vip,tuan:tuan});
    c.ajax(
      app.url + "toGoodIdGetGoodInfo", {
        goods_id: goods_id,
        user_id: user_id
      },
      res => {
        that.setData({
          ginfo: res.data.finfo,
          uinfo: res.data.uinfo,
          ordernum: res.data.ordernum
        })
        var ordernum =res.data.ordernum;
        c.ajax(
          app.url + 'goodsOrderGetNum', {
            user_id: user_id,
            goods_id: goods_id,
            vip: vip,
            tuan: tuan,
            ordernum: ordernum
          },
          res => {
            var orderid=res.data;
            that.setData({
              orderid: orderid
            })
          }
        )
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  goumai: function() {
    this.setData({
      showPayPwdInput: true
    })
  },
  /**
   * 显示支付密码输入层
   */
  showInputLayer: function() {
    this.setData({
      showPayPwdInput: false,
      payFocus: true
    });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePay: function() {

    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: ''
    });

  },

  /**
   * 隐藏支付密码输入层 并且获取输入的密码
   */
  // goods_id: goods_id,
  // money: money,
  // vip: vip,
  // tuan: tuan
  hidePayLayer: function() {
    var val = this.data.pwdVal;
    var user_id=app.u.user_id;
    var goods_id = this.data.goods_id;
    var vip = this.data.vip;
    var tuan= this.data.tuan;
    var orderid = this.data.orderid;
  
    if (typeof (vip)=='undefined'){
           vip=0;
    }
    if (typeof (tuan) == 'undefined') {
           tuan = 0;
    }
    this.setData({
      showPayPwdInput: false,
      payFocus: false,
      pwdVal: ''
    }, function() {
      c.ajax(
        app.url + 'goodsOrderPar', {
          user_id: user_id,
          goods_id: goods_id,
          vip: vip,
          tuan: tuan,
          orderid: orderid
        },
        res => {
          if (res.data) {
            wx.navigateTo({
              url: '/pages/sendneedsuccess/sendneedsuccess?typeinfo=3',
            })
          } else {
            wx.showToast({
              title: '支付密码错误',
              icon: 'none'
            })
          }
        }
      )

    });

  },
  /**
   * 获取焦点
   */
  getFocus: function() {
    this.setData({
      payFocus: true
    });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function(e) {
    this.setData({
      pwdVal: e.detail.value
    });

    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
    }
  },
  passwordset: function() {
    wx.navigateTo({
      url: '/pages/sendneedsetpass/sendneedsetpass',
    })
  }
})